package com.mall.admin.controller.goods;

import java.util.List;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.Kv;
import com.mall.admin.controller.AdminBaseController;
import com.mall.admin.service.goods.GoodsPropertysService;
import com.mall.common.annotation.Inject;
import com.mall.common.model.goods.GoodsProperty;
import com.mall.common.model.goods.GoodsPropertyValue;
import com.mall.common.service.base.Result;
import com.mall.common.util.ResultCode;

/**
 * 商品属性管理
 * @author zengjintao
 * @version 1.0
 * @createTime 2018年8月5日下午12:06:14
 */
@ControllerBind(controllerKey="/admin/goods/property")
public class GoodsPropertyController extends AdminBaseController{

	@Inject
	private GoodsPropertysService goodsPropertysService;
	
	@Override
	public void index() {
		renderView("/goods/property/index.vm");
	}
	
	/**
	 * 属性列表
	 */
    public void list(){
    	int pageNumber = getParaToInt("pageNumber",1);
    	int pageSize = getParaToInt("pageSize",10);
    	String name = getPara("name");
    	Kv condition = Kv.by("name", name);
    	Result result = goodsPropertysService.list(pageSize,pageNumber,condition);
		setAttr("page", result.getDefaultModel());
		renderView("/goods/property/list.vm");
	}
    
    /**
     * 添加属性值
     */
    public void addPropertyValue(){
    	renderView("/goods/property/propertyForm.vm");
    }
    
    /**
     * 创建属性值
     */
    public void createPropertyValue(){
    	int propertyId = getParaToInt("propertyId",0);
    	GoodsPropertyValue goodsPropertyValue = getModel(GoodsPropertyValue.class,"goodsPropertyValue");
    	goodsPropertyValue.set("goods_property_id", propertyId);
    	ResultCode resultCode = goodsPropertysService.createPropertyValue(goodsPropertyValue);
    	renderJson(resultCode);
    }

    /**
     * 添加属性
     */
	public void add(){
		renderView("/goods/property/add.vm");
	}
	
	/**
     * 创建属性
     */
    public void create(){
    	GoodsProperty goodsProperty = getModel(GoodsProperty.class,"goodsProperty");
    	ResultCode resultCode = goodsPropertysService.createProperty(goodsProperty);
    	renderJson(resultCode);
	}
    
    /**
     * 删除属性
     */
    public void delete(){
    	int id = getParaToInt("id");
    	ResultCode resultCode = goodsPropertysService.deleteById(id);
    	renderJson(resultCode);
    	
    }
	
    /**
     * 编辑属性
     */
    public void edit(){
    	int id = getParaToInt("id");
    	GoodsProperty goodsProperty = GoodsProperty.dao.findById(id);
    	setAttr("goodsProperty", goodsProperty);
    	renderView("/goods/property/edit.vm");
	}
	
    /**
     * 更新属性
     */
    public void update(){
    	int id = getParaToInt(0);
    	GoodsProperty goodsProperty = getModel(GoodsProperty.class,"goodsProperty");
    	goodsProperty.set("id", id);
    	ResultCode resultCode = goodsPropertysService.update(goodsProperty);
    	renderJson(resultCode);
	}
    
    /**
     * 删除属性值
     */
    public void deletePropertyValue(){
    	int id = getParaToInt("id");
    	ResultCode resultCode = goodsPropertysService.deletePropertyValue(id);
    	renderJson(resultCode);
    }
    
    
    /**
	 * 查找属性值列表
	 * @return
	 */
    public void findByProperty(){
    	int propertyId = getParaToInt("id");
    	List<GoodsPropertyValue> list = goodsPropertysService.findByProperty(propertyId);
    	renderJson(list);
    }
}
